package com.zx.easy.converter;

import cn.hutool.core.util.ObjectUtil;
import com.zx.easy.enums.EnumManager;
import com.zx.easy.enums.LanguagesEnum;
import com.zx.easy.pojo.dto.UserReadDto;
import com.zx.easy.pojo.entity.User;
import com.zx.easy.pojo.vo.UserExportVo;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;

import java.util.List;

@Mapper(componentModel = "spring",uses = {StructHelper.class})
public interface MapStructConverter {



    @Mappings({
            @Mapping(source = "timestamp",target = "timestamp",qualifiedByName = StructConstant.TIME_MILLIS_TO_DATE_STR),
            @Mapping(target = "languages",expression = "java(getLanguagesEnum(entity))")
    })
    UserExportVo userToUserVo(User entity);
    
    List<UserExportVo> userListToVoList(List<User> userList);
    
    
    @Mappings({
            @Mapping(source = "timestamp",target = "timestamp",qualifiedByName = StructConstant.DATE_STR_TO_TIME_MILLIS),
            @Mapping(source = "createTime",target="createTime",qualifiedByName=StructConstant.DATE_STR_TO_LOCAL_DATE),
            @Mapping(source = "updateTime",target="updateTime",qualifiedByName=StructConstant.DATE_STR_TO_LOCAL_DATE)
    })
    User userVoToUserEntity(UserReadDto vo );

    List<User> userVoListToUserList(List<UserReadDto> voList);
    
    
    default  String getLanguagesEnum(User entity){
        if (ObjectUtil.isNull(entity.getLanguages())){
            return "";
        }
        
       return EnumManager.getCodeName(LanguagesEnum.class,entity.getLanguages());
    }
}


